package com.booklove.service.school;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.po.School;
import com.booklove.domain.query.SchoolQuery;
import com.booklove.mapper.SchoolMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 学校信息 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-21
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService {

    @Override
    public PageDTO<School> querySchoolsPage(SchoolQuery schoolQuery) {
        String name = schoolQuery.getName();
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则
        List<OrderItem> orderItems = schoolQuery.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<School> page = schoolQuery.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
        /*
        使用lambda的方式查询
         */
        Page<School> p = lambdaQuery()
                .like(name != null, School::getSchoolName, name)
                .page(page);

        // 获取数据信息

        return PageDTO.of(p, School.class);
    }
}
